package com.example.pda.view;

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.LinearLayoutCompat;
import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.RecyclerView;

public class MyScrollerLayout extends NestedScrollView {

    private RecyclerView recyclerView;

    public MyScrollerLayout(@NonNull Context context) {
        super(context);
    }

    public MyScrollerLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public MyScrollerLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setRecyclerView(RecyclerView recyclerView) {
        this.recyclerView = recyclerView;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        LinearLayoutCompat parent = (LinearLayoutCompat) recyclerView.getParent();
        if (parent != null && parent.getVisibility() == VISIBLE) {
            int x = (int) event.getX();
            int y = (int) event.getX();
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                Rect rect = new Rect();
                recyclerView.getDrawingRect(rect);
                if (rect.contains(x, y)) {
                    parent.setVisibility(View.GONE);
                    return false;
                }
            }
        }
        return super.onInterceptTouchEvent(event);
    }
}
